#import "DictPageViewPlatformView.h"
#import "ResultPageViewController.h"

static NSMutableDictionary* s_views = NULL;

@implementation DictPageViewPlatformView {
    ResultPageViewController* _vc;
    int64_t _viewId;
}

+(DictPageViewPlatformView*)findViewByViewIdentifier:(int64_t)viewId {
    if (!s_views) return NULL;
    NSString* key = [NSString stringWithFormat:@"%ld", viewId];
    return [s_views valueForKey:key];
}

- (void) createWithFrame:(CGRect) frame
          viewIdentifier:(int64_t)viewId
               arguments:(id _Nullable)args {
    [super init];
    _viewId = viewId;
    _vc = [[ResultPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
    if (!s_views) s_views = [[NSMutableDictionary alloc] init];
    NSString* key = [NSString stringWithFormat:@"%ld", viewId];
    [_vc setPlatformViewId:_viewId];
    [s_views setValue:self forKey:key];
}

- (UIView *)view {
    return _vc.view;
}

- (void)dealloc {
    [self removeFromView];
}
- (void)removeFromView {
    NSString* key = [NSString stringWithFormat:@"%ld", _viewId];
    [s_views removeObjectForKey:key];
    [_vc.view removeFromSuperview];
}

- (void)loadTab:(NSArray*)data {
    if (_vc) [_vc loadInfo:data];
}

- (void)loadUrl:(NSString*)url atIndex:(int)index {
    if (_vc) [_vc loadUrl:url atIndex:index];
}
- (void)changeTab:(int)index {
    if (_vc) [_vc changeTab:index];
}
- (NSData*)takePhoto:(int)index {
    if (_vc) [_vc takePhotoAt:index];
    return NULL;
}
@end